<template>
  <div>
    请开始搜索:<input type="text" v-model="value" @keydown="clickRun" />
  </div>
</template>

<script>
/**
 * @desc 函数防抖
 * @param func 目标函数
 * @param wait 延迟执行毫秒数
 * @param immediate true - 立即执行， false - 延迟执行
 */
function debounce(func, wait, immediate) {
  let timer;
  wait = wait || 1000;
  return function() {
    let context = this,
      args = arguments;
    if (timer) clearTimeout(timer);
    if (immediate) {
      let callNow = !timer;
      timer = setTimeout(() => {
        timer = null;
      }, wait);
      if (callNow) func.apply(context, args);
    } else {
      timer = setTimeout(() => {
        func.apply(context, args);
      }, wait);
    }
  };
}
export default {
  data() {
    return {
      value: ""
    };
  },
  methods: {
    //3个参数，第二，三个参数可以省略，默认为1000，false
    clickRun: debounce(function() {
      console.log(this.value);
    })
  }
};
</script>

<style lang="scss" scoped></style>
